import {axiosClient} from "@/utils/axiosClient.js";
import {handleCommonParams} from "@/api/commonParams.js";

const requestUrl = '/number/numberRule'

const api = {

    /**
     * 新增编号规则定义
     * @param numberRule
     * @returns {Promise<*|undefined>}
     */
    createNumberRule: (numberRule) =>
      axiosClient().post(`${requestUrl}/addInfo`, numberRule),

    /**
     * 根据ID删除编号规则定义
     * @param id
     * @returns {Promise<*|undefined>}
     */
    deleteNumberRuleById: (id) =>
        axiosClient().remove(`${requestUrl}/removeInfo/${id}`),

    /**
     * 根据ID全量更新编号规则定义
     * @param id
     * @param numberRule
     * @returns {Promise<*|undefined>}
     */
    updateNumberRuleFull: (id, numberRule) =>
        axiosClient().put(`${requestUrl}/modifyInfo/${id}`, numberRule),

    /**
     * 根据ID部分更新编号规则定义
     * @param id
     * @param updatePartial
     * @returns {Promise<*|undefined>}
     */
    updateNumberRulePartial: (id, updatePartial) =>
        axiosClient().patch(`${requestUrl}/modifyPartialInfo/${id}`, updatePartial),

    /**
     * 根据ID查询编号规则定义
     * @param id
     * @returns {Promise<*|undefined>}
     */
    getNumberRuleById: (id) =>
        axiosClient().get(`${requestUrl}/getInfo/${id}`),

    /**
     * 分页查询编号规则定义
     * @param page
     * @param size
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getNumberRulePage: (page, size, businessParam) => {
        const params = new URLSearchParams();
        params.append('page', page)
        params.append('size', size)
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfos?${params.toString()}`)
    },

    /**
     * 根据条件查询编号规则定义
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getNumberRuleList: (businessParam) => {
        const params = new URLSearchParams();
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfoList?${params.toString()}`)
    }

}

const setBusinessParams = (params, businessParam) => {
    if (businessParam) {
        if (businessParam.ruleCode) {
            params.append('ruleCode', businessParam.ruleCode)
        }
        if (businessParam.ruleName) {
            params.append('ruleName', businessParam.ruleName)
        }
        if (businessParam.bizType) {
            params.append('bizType', businessParam.bizType)
        }
        if (businessParam.description) {
            params.append('description', businessParam.description)
        }
        if (businessParam.defaultFlag) {
            params.append('defaultFlag', businessParam.defaultFlag)
        }
        handleCommonParams(params, businessParam);
    }
}

export default api
